﻿using System;
using System.Linq;
using Telerik.Web.UI;
using Thuoc24h.Business;
using Thuoc24h.Common;
using Thuoc24h.Data;

namespace Thuoc24h.AdminCP.Pages
{
    public partial class ConfigsEdit : BasePage.BasePages
    {
        private int _id;
        private Config _obj = new Config();

        protected void Page_Load(object sender, EventArgs e)
        {
            _id = Convert.ToInt32(Request.QueryString["ID"]);
            if (!Page.IsPostBack)
            {

                if (_id > 0)
                {
                    txtName.Enabled = false;
                    edtDescription.Enabled = false;
                    btnSave.Text = ActionNames.Update;
                    BindData();
                }
                else
                {
                    btnSave.Text = ActionNames.AddNew;
                }

                BindDataToGrid();
            }

        }

        private void GetData()
        {
            _obj.Name = txtName.Text;
            _obj.Description = edtDescription.Text;
            _obj.Data = edtDetail.Content;
        }
        private void Reset()
        {
            edtDescription.Text = string.Empty;
            txtName.Text = string.Empty;
            edtDetail.Content = string.Empty;
        }
        private void SaveData()
        {
            if (_id > 0)
            {
                _obj = context.Configs.SingleOrDefault(item => item.Id == _id);
                if (_obj != null)
                {
                    GetData();
                }

            }
            else
            {
                GetData();
                context.Configs.AddObject(_obj);
            }

            if (context.SaveChanges() > 0)
            {

                Reset();
                Response.Redirect(UrlManager.Admin.ConfigsEdit);
            }
        }
        private void BindData()
        {
            _obj = ReviewRepository.ConfigById(_id);
            txtName.Text = _obj.Name;
            edtDescription.Text = _obj.Description;
            edtDetail.Content = _obj.Data;
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            SaveData();

        }

        protected void OnClick(object sender, EventArgs e)
        {
            Reset();
        }

        private void BindDataToGrid()
        {
            gridData.DataSource = ReviewRepository.GetAllConfigs();
            gridData.DataBind();
        }

        protected void gridData_ItemCommand(object sender, GridCommandEventArgs e)
        {
            string id = string.Empty;

            var commandName = e.CommandName;
            var gridDataItem = e.Item as GridDataItem;
            if (gridDataItem != null)
            {
                id = gridDataItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"].ToString();
            }

            switch (commandName)
            {

                case CommandNames.Delete:
                    ReviewRepository.ConfigDelete(id.ToInt32Return0());
                    BindDataToGrid();
                    break;
                case CommandNames.Edit:
                    Utils.Redirect(UrlManager.Admin.ConfigsEdit, id);
                    break;

            }


        }
    }
}